using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace Impact.Model.Objects
{
	/// <summary>
	/// Strongly-typed collection for the EmailTemplate class.
	/// </summary>
    [Serializable]
	public partial class EmailTemplateCollection : ActiveList<EmailTemplate, EmailTemplateCollection>
	{	   
		public EmailTemplateCollection() {}
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the impact_EmailTemplates table.
	/// </summary>
	[Serializable]
	public partial class EmailTemplate : ActiveRecord<EmailTemplate>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public EmailTemplate()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public EmailTemplate(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public EmailTemplate(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public EmailTemplate(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("impact_EmailTemplates", TableType.Table, DataService.GetInstance("ImpactProvider"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarTemplateID = new TableSchema.TableColumn(schema);
				colvarTemplateID.ColumnName = "TemplateID";
				colvarTemplateID.DataType = DbType.Int32;
				colvarTemplateID.MaxLength = 0;
				colvarTemplateID.AutoIncrement = true;
				colvarTemplateID.IsNullable = false;
				colvarTemplateID.IsPrimaryKey = true;
				colvarTemplateID.IsForeignKey = false;
				colvarTemplateID.IsReadOnly = false;
				colvarTemplateID.DefaultSetting = @"";
				colvarTemplateID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTemplateID);
				
				TableSchema.TableColumn colvarName = new TableSchema.TableColumn(schema);
				colvarName.ColumnName = "Name";
				colvarName.DataType = DbType.AnsiString;
				colvarName.MaxLength = 50;
				colvarName.AutoIncrement = false;
				colvarName.IsNullable = false;
				colvarName.IsPrimaryKey = false;
				colvarName.IsForeignKey = false;
				colvarName.IsReadOnly = false;
				colvarName.DefaultSetting = @"";
				colvarName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarName);
				
				TableSchema.TableColumn colvarSubject = new TableSchema.TableColumn(schema);
				colvarSubject.ColumnName = "Subject";
				colvarSubject.DataType = DbType.AnsiString;
				colvarSubject.MaxLength = 500;
				colvarSubject.AutoIncrement = false;
				colvarSubject.IsNullable = true;
				colvarSubject.IsPrimaryKey = false;
				colvarSubject.IsForeignKey = false;
				colvarSubject.IsReadOnly = false;
				colvarSubject.DefaultSetting = @"";
				colvarSubject.ForeignKeyTableName = "";
				schema.Columns.Add(colvarSubject);
				
				TableSchema.TableColumn colvarBody = new TableSchema.TableColumn(schema);
				colvarBody.ColumnName = "Body";
				colvarBody.DataType = DbType.AnsiString;
				colvarBody.MaxLength = 2147483647;
				colvarBody.AutoIncrement = false;
				colvarBody.IsNullable = true;
				colvarBody.IsPrimaryKey = false;
				colvarBody.IsForeignKey = false;
				colvarBody.IsReadOnly = false;
				colvarBody.DefaultSetting = @"";
				colvarBody.ForeignKeyTableName = "";
				schema.Columns.Add(colvarBody);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["ImpactProvider"].AddSchema("impact_EmailTemplates",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("TemplateID")]
		[Bindable(true)]
		public int TemplateID 
		{
			get { return GetColumnValue<int>(Columns.TemplateID); }
			set { SetColumnValue(Columns.TemplateID, value); }
		}
		  
		[XmlAttribute("Name")]
		[Bindable(true)]
		public string Name 
		{
			get { return GetColumnValue<string>(Columns.Name); }
			set { SetColumnValue(Columns.Name, value); }
		}
		  
		[XmlAttribute("Subject")]
		[Bindable(true)]
		public string Subject 
		{
			get { return GetColumnValue<string>(Columns.Subject); }
			set { SetColumnValue(Columns.Subject, value); }
		}
		  
		[XmlAttribute("Body")]
		[Bindable(true)]
		public string Body 
		{
			get { return GetColumnValue<string>(Columns.Body); }
			set { SetColumnValue(Columns.Body, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
				
		private Impact.Model.Objects.EventCollection colEvents;
		public Impact.Model.Objects.EventCollection Events
		{
			get
			{
				if(colEvents == null)
				{
					colEvents = new Impact.Model.Objects.EventCollection().Where(Event.Columns.EmailTemplateID, TemplateID).Load();
					colEvents.ListChanged += new ListChangedEventHandler(colEvents_ListChanged);
				}
				return colEvents;			
			}
			set 
			{ 
					colEvents = value; 
					colEvents.ListChanged += new ListChangedEventHandler(colEvents_ListChanged);
			}
		}
		
		void colEvents_ListChanged(object sender, ListChangedEventArgs e)
		{
		    if (e.ListChangedType == ListChangedType.ItemAdded)
		    {
		        // Set foreign key value
		        colEvents[e.NewIndex].EmailTemplateID = TemplateID;
		    }
		}
		
		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varName,string varSubject,string varBody)
		{
			EmailTemplate item = new EmailTemplate();
			
			item.Name = varName;
			
			item.Subject = varSubject;
			
			item.Body = varBody;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varTemplateID,string varName,string varSubject,string varBody)
		{
			EmailTemplate item = new EmailTemplate();
			
				item.TemplateID = varTemplateID;
			
				item.Name = varName;
			
				item.Subject = varSubject;
			
				item.Body = varBody;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn TemplateIDColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn NameColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn SubjectColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn BodyColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string TemplateID = @"TemplateID";
			 public static string Name = @"Name";
			 public static string Subject = @"Subject";
			 public static string Body = @"Body";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
                if (colEvents != null)
                {
                    foreach (Impact.Model.Objects.Event item in colEvents)
                    {
                        if (item.EmailTemplateID != TemplateID)
                        {
                            item.EmailTemplateID = TemplateID;
                        }
                    }
               }
		}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
                if (colEvents != null)
                {
                    colEvents.SaveAll();
               }
		}
        #endregion
	}
}
